import { getProductList } from '../../service/common.js';
Page({
  data: {
    imgArr: [],
    prveIndex: -1, // 上张图
    swiperIndex: 0, // 当前
    nextIndex: -1, // 下一张
    height:'',
    bottom:''
  },
  onLoad(){
    this._getProductList()
    let _this = this
    wx.getSystemInfo({
      success(res) {
        console.log(res)
        let clientHeight = res.windowHeight;
        let clientWidth = res.windowWidth;
        let ratio = 750 / clientWidth;
        let height = clientHeight * ratio;
        // console.log(clientHeight)
        _this.setData({ height: height-100 });
        _this.setData({ height1: (height-100)*958/1028});
      }
    });
  },
  // onShow() {
  //   this._getProductList()
  // },
  goto(){
    wx.navigateTo({
      url: '../detail/detail?id=' + this.data.imgArr[this.data.swiperIndex].id,
    })
  },
  //滑动图片
  swiperChange(e) {
    let len = this.data.imgArr.length
    let swiperIndex = e.detail.current
    this.setData({
      swiperIndex
    })
    if (len > 1) {
      this.setData({
        nextIndex: swiperIndex < len ? swiperIndex + 1 : 0,
        prveIndex: swiperIndex > 0 ? swiperIndex - 1 : len,
      })
    }
  },
  _getProductList(){
    getProductList().then(res=>{
      if(res.code==200){
        this.setData({ imgArr: res.data.categories })
        let obj = {
          detail:{
            current:0
          }
        }
        var query = wx.createSelectorQuery();	// 创建节点查询器 query
        // let
        //选择id
        query.select('.nowImg').boundingClientRect( rect=> {
          console.log(rect)
          this.setData({ bottom: rect.bottom})
        }).exec();
        query.selectViewport().scrollOffset().exec()//这段代码的意思是获取页面滑动位置的查询请求
        this.swiperChange(obj)
      }
    })
  }
})